<?php
namespace App\Http\Controllers\Home;

use App\Http\Model\Navs;
use App\Http\Model\Article;
use App\Http\Model\Links;
use App\Http\Model\Category;


class IndexController extends CommonController
{	
    public function index(){
    	//点击量最高的6篇文章
		$hot = Article::orderBy('art_view','desc')->take(6)->get();

		//点击排行的5篇文章
		$pics = Article::orderBy('art_view','desc')->take(5)->get();

		//图文列表5篇(带分页)
		$data = Article::orderBy('art_time','desc')->paginate(5);

		//最新发布文章8条
		$new = Article::orderBy('art_time','desc')->take(8)->get();

		//友情链接
		$links = Links::orderBy('link_order','asc')->get();

    	$navs = Navs::orderBy('nav_order','asc')->get();
    	return view('home.index',compact('hot','pics','data','new','links','navs'));
    }

    public function cate($cate_id){

    	//图文列表5篇(带分页)
		$data = Article::where('cate_id',$cate_id)->orderBy('art_time','desc')->paginate(4);
        
		//当前分类的子分类
		$submenu = Category::where('cate_pid',$cate_id)->get();

		//最新发布文章8条
		$new = Article::orderBy('art_time','desc')->take(8)->get();

		//点击排行的5篇文章
		$pics = Article::orderBy('art_view','desc')->take(5)->get();

		//分类查看次数自增
    	Category::where('cate_id',$cate_id)->increment('cate_view');

    	$field = Category::find($cate_id);
    	$navs = Navs::orderBy('nav_order','asc')->get();
    	return view('home.list',compact('data','field','navs','submenu','new','pics'));
    }

    public function article($art_id){
    	//连表查询cata_id相同的
    	$field = Article::Join('category','article.cate_id','=','category.cate_id')->where('art_id',$art_id)->first();

    	/*dd($field);*/
    	//点击排行的5篇文章
		$pics = Article::orderBy('art_view','desc')->take(5)->get();

		//最新发布文章8条
		$new = Article::orderBy('art_time','desc')->take(8)->get();
		//导航
    	$navs = Navs::orderBy('nav_order','asc')->get();

    	//上一篇下一篇
    	$article['pre'] = Article::where('art_id','<',$art_id)->orderBy('art_id','desc')->first();
    	$article['next'] = Article::where('art_id','>',$art_id)->orderBy('art_id','asc')->first();
    	// dd($article['next']['art_title']);\
    	
    	//相关文章
    	$data = Article::where('cate_id',$field->cate_id)->orderBy('art_id','desc')->take(6)->get();

    	//文章查看次数自增
    	Article::where('art_id',$art_id)->increment('art_view');

    	return view('home.new',compact('navs','field','new','pics','article','data'));
    }
}
